package com.sh.caryr.erp.service.impl.order;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sh.caryr.erp.entity.definition.CommonFieldDef;
import com.sh.caryr.erp.entity.enums.OrderStatus;
import com.sh.caryr.erp.entity.trade.AsOrder;
import com.sh.caryr.erp.entity.trade.Order;
import com.sh.caryr.erp.mapper.order.AsOrderMapper;
import com.sh.caryr.erp.service.IOrderService;
import com.sh.caryr.erp.service.order.IAsOrderService;
import com.sh.caryr.erp.util.MyBatisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class AsOrderServiceImpl extends ServiceImpl<AsOrderMapper, AsOrder> implements IAsOrderService {

    @Autowired
    private IOrderService orderService;

    @Override
    @Transactional
    @SuppressWarnings("unchecked")
    public boolean insertOrUpdate(AsOrder entity) {
        Wrapper wrapper = MyBatisUtil.getIsActiveWrapper().
                eq(CommonFieldDef.ORDER_EXT_TXN_ID, entity.getExtOrderId()).
                eq(CommonFieldDef.EXT_PLATFORM, entity.getExtPlatform());
        Order order = orderService.selectOne(wrapper);
        if (order != null) {
            order.setStatus(OrderStatus.RETURN.getValue());
            order.setReturnType(entity.getReturnType());
            orderService.updateById(order);
            entity.setOrderId(order.getId());
        }
        return super.insertOrUpdate(entity);
    }

}
